package com.hotelvip.controller;


import com.hotelvip.common.pojo.PageObject;
import com.hotelvip.common.utils.JSONResult;
import com.hotelvip.entity.SysUser;
import com.hotelvip.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Lzh
 * @date 2020/11/30 14:30
 */
@RestController
@RequestMapping(("/sys/user"))
@Api(basePath = "/sys/user",tags = "系统用户模块")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    /**
     * 修改用户密码
     * @param sourcePassword
     * @param newPassword
     * @param cfgPassword
     * @return
     */
    @ApiOperation(value = "修改密码",httpMethod = "PUT")
    @PutMapping("/updateUserPassword")
    public JSONResult updateUserPassword(String sourcePassword,String newPassword,String cfgPassword){
        sysUserService.updateUserPassword(sourcePassword,newPassword,cfgPassword);
        return JSONResult.ok("修改成功");
    }
    @ApiOperation(value = "按照id查找用户",httpMethod = "GET")
    @GetMapping("/doFindUserById/{id}")
    public JSONResult doFindUserById(@PathVariable("id") Integer id){
        return JSONResult.ok(sysUserService.findUserById(id));
    }

    /**
     * 修改用户信息
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "修改用户信息",httpMethod = "PUT")
    @PutMapping("/updateUser")
    public JSONResult updateUser(@RequestBody SysUser sysUser){
        sysUserService.updateUser(sysUser);
        return JSONResult.ok();
    }


//    public JSONResult doLogin(String username,String password,boolean isRememberMe){
//        //获取用户信息
//        Subject subject = SecurityUtils.getSubject();
//        //提交账号信息
//        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//        token.setRememberMe(isRememberMe);
//        subject.login(token);
//        Map<String, Object> chaim = new HashMap<>();
//        chaim.put("username", username);
//        String jwtToken = jwtUtil.encode(token.getUsername(), 10080, chaim);
//        sysUserService.doLogin(username);
//        return JSONResult.ok(jwtToken);
//    }

    @ApiOperation(value = "修改用户状态",httpMethod = "PUT")
    @PutMapping("/updateStatus")
    public JSONResult updateStatus(Integer id,Integer status){
        sysUserService.updateStatus(id,status);
        return JSONResult.ok();
    }


    @GetMapping("/doFindSysUser/{pageCurrent}/{pageSize}")
    public JSONResult doFindSysUser(
            @PathVariable("pageCurrent") Integer pageCurrent,
            @PathVariable("pageSize") Integer pageSize){
        PageObject<SysUser> sysUsers = sysUserService.findSysUsers(pageCurrent, pageSize);
        return JSONResult.ok(sysUsers);
    }

}
